import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Demo3 {
    //升级版
    public static void main(String[] args) throws IOException {
        try(InputStream inputStream = new FileInputStream("e:/hello.txt")) {
            while(true) {
            byte[] bytes = new byte[1024];
            int n = inputStream.read(bytes);
                if (n == -1) {
                    break;
                }
            for (int i = 0; i < n; i++) {
                System.out.printf("0x%x ",bytes[i]);
            }
            }
        }
    }
    //不够优雅
    public static void main1(String[] args) throws IOException {
        InputStream inputStream = null;
      try {
          inputStream = new FileInputStream("e:/hello.txt");//1、字符串表示的文件路径2、File对象
//        while(true) {
//            int b = inputStream.read();//一次读一个字节
//            if(b == -1) {//读取完毕
//                break;
//            }
//            System.out.printf("0x%x ",b);//十六进制
//        }
          while (true) { //如果文件非常长，超过1024，需要搭配循环，读若干次
              byte[] bytes = new byte[1024];
              int n = inputStream.read(bytes);//输出型参数
             // System.out.println(n);
              if (n == -1) {
                  break;
              }
              for (int i = 0; i < n; i++) {
                  System.out.printf("0x%x ", bytes[i]);
              }
             // System.out.println();
          }
      }finally {
          inputStream.close();
      }
    }
}
